﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

using WebSite.Domain;
using System.Web.UI.WebControls;

namespace WebSite.DAL {
    public class RespostaProntuarioDAO
    {     

        public static void CadastrarRespostaContinua(RespostaProntuario repostaProntuario)
        {
            string strsql = "INSERT INTO tab_RespostaProntuario(respostaContinua, idItemProntuario,idCasoPaciente) VALUES (@respostaContinua,@idItemProntuario,(select IDENT_CURRENT('SistemaProntuario.dbo.tab_CasoPaciente')));";
            SqlConnection objConnection = ConnectionFactory.obterConexao();
            objConnection.Open();
            SqlCommand objCommand = new SqlCommand(strsql, objConnection);
            objCommand.Parameters.Add(new SqlParameter("@respostaContinua", repostaProntuario.RespostaContinua));            
            objCommand.Parameters.Add(new SqlParameter("@idItemProntuario", repostaProntuario.idItemProntuario));
            objCommand.Parameters.Add(new SqlParameter("@idCasoPaciente", repostaProntuario.idCasoPaciente));
            objCommand.ExecuteNonQuery();
            objConnection.Close();
        }

        public static void CadastrarRespostaDiscreta(RespostaProntuario repostaProntuario)
        {
            string strsql = "INSERT INTO tab_RespostaProntuario(idRespostaDiscreta, idItemProntuario,idCasoPaciente) VALUES (@idRespostaDiscreta,@idItemProntuario,(select IDENT_CURRENT('SistemaProntuario.dbo.tab_CasoPaciente')));";
            SqlConnection objConnection = ConnectionFactory.obterConexao();
            objConnection.Open();
            SqlCommand objCommand = new SqlCommand(strsql, objConnection);            
            objCommand.Parameters.Add(new SqlParameter("@idRespostaDiscreta", repostaProntuario.idRepostaDiscreta));
            objCommand.Parameters.Add(new SqlParameter("@idItemProntuario", repostaProntuario.idItemProntuario));
            objCommand.Parameters.Add(new SqlParameter("@idCasoPaciente", repostaProntuario.idCasoPaciente));
            objCommand.ExecuteNonQuery();
            objConnection.Close();
        }
        public static List<RespostaProntuario> BuscarRespostasProntuario(int idCasoPaciente)
        {
            SqlDataReader dr;
            string strsql = "Select * from tab_RespostaProntuario where idCasoPaciente = @idCasoPaciente";
            SqlConnection objConnection = ConnectionFactory.obterConexao();
            List<RespostaProntuario> respostaProntuario;

            try
            {
                objConnection.Open();
                SqlCommand objCommand = new SqlCommand(strsql, objConnection);
                objCommand.Parameters.Add(new SqlParameter("@idCasoPaciente", idCasoPaciente));

                dr = objCommand.ExecuteReader();
                respostaProntuario = new List<RespostaProntuario>();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        RespostaProntuario rp = new RespostaProntuario();
                        rp.idRespostaProntuario = Convert.ToInt32(dr["idRespostasProntuarios"]);

                        if (dr["respostaContinua"] == DBNull.Value)
                            rp.RespostaContinua = string.Empty;
                        else
                            rp.RespostaContinua =  dr["respostaContinua"].ToString();

                        if (dr["idRespostaDiscreta"] == DBNull.Value)
                            rp.idRepostaDiscreta = 0;
                        else
                            rp.idRepostaDiscreta = Convert.ToInt32(dr["idRespostaDiscreta"]);

                        //ITEM A ALTERAR, IDITEMPRONTUARIO NAO PODE SER NULL
                        if (dr["idItemProntuario"] == DBNull.Value)
                            rp.idItemProntuario = 0;
                        else
                            rp.idItemProntuario = Convert.ToInt32(dr["idItemProntuario"]);

                        rp.idCasoPaciente = Convert.ToInt32(dr["idCasoPaciente"]);

                        respostaProntuario.Add(rp);
                    }
                }
                else
                {
                    respostaProntuario = null;
                }

                return respostaProntuario;

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                objConnection.Close();
            }            
        }


    }
}